﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LaserHarp
{
    public delegate void NoteChangedHandler(object sender, EventArgs e);

    class ComboArray : System.Collections.CollectionBase
    {
        public event NoteChangedHandler ChangeNote;

        private Control container;

        public ComboArray(Control host)
        {
            container = host;
        }

        public ComboBox AddNewCombo()
        {
            // Create a new instance of the Button class.
            ComboBox aCombo = new ComboBox();
            // Add the button to the collection's internal list.
            this.List.Add(aCombo);
            // Add the button to the controls collection of the form 
            // referenced by the HostForm field.
            container.Controls.Add(aCombo);
            // Set intial properties for the button object.
            aCombo.Top = 45;
            aCombo.Left = 60 + 50 * (Count - 1);
            aCombo.Width = 45;
            aCombo.Tag = this.Count - 1;
            aCombo.Text = "";
            aCombo.SelectedIndexChanged += new System.EventHandler(ChangeSelected);

            //Fill list with note names
            for (int i = 0; i < 128; i++)
            {
                String name = "";
                switch (i % 12)
                {
                    case 0:
                        name = "C";
                        break;
                    case 1:
                        name = "C#";
                        break;
                    case 2:
                        name = "D";
                        break;
                    case 3:
                        name = "D#";
                        break;
                    case 4:
                        name = "E";
                        break;
                    case 5:
                        name = "F";
                        break;
                    case 6:
                        name = "F#";
                        break;
                    case 7:
                        name = "G";
                        break;
                    case 8:
                        name = "G#";
                        break;
                    case 9:
                        name = "A";
                        break;
                    case 10:
                        name = "A#";
                        break;
                    case 11:
                        name = "B";
                        break;
                }
                int octave = (i / 12) - 1;
                name += octave.ToString();
                aCombo.Items.Add(name);
            }

            return aCombo;
        }

        public ComboBox this[int Index]
        {
            get
            {
                return (ComboBox)this.List[Index];
            }
        }

        public void Remove()
        {
            // Check to be sure there is a button to remove.
            if (this.Count > 0)
            {
                // Remove the last button added to the array from the host form 
                // controls collection. Note the use of the indexer in accessing the array.
                container.Controls.Remove(this[this.Count - 1]);
                this.List.RemoveAt(this.Count - 1);
            }
        }

        public void ChangeSelected(Object sender, System.EventArgs e)
        {
            ChangeNote(sender, e);
        }
    }
}
